package com.awccis.common.pool;

import com.awccis.common.session.CustomerSession;

import java.util.*;

/**
 * @author: txp
 * @date: 2018/5/27 on 下午9:26
 * @since: 1.0
 * @description: customerPool
 */
public class CustomerPool {

    /**
     * map集合作为池子，存取乘客信息
     */
    private static Map<String, CustomerSession> map = new HashMap<String, CustomerSession>();

    /**
     * 乘客上线，存入池中
     *
     * @param sessionId       用户登录的sessionId
     * @param customerSession 用户登录的session
     */
    public static void addCustomerSession(String sessionId, CustomerSession customerSession) {
        if (!map.containsKey(sessionId)) {
            map.put(sessionId, customerSession);
        }
    }

    /**
     * 删除乘客session
     *
     * @param sessionId sessionId
     */
    public static void deleteCustomerSession(String sessionId) {
        if (map.containsKey(sessionId)) {
            map.remove(sessionId);
        }
    }

    /**
     * 通过手机号删除用户在线信息
     *
     * @param phone phone
     * @return 执行结果
     */
    public static boolean deleteCustomerByPhone(String phone) {
        Iterator<Map.Entry<String, CustomerSession>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, CustomerSession> entry = entries.next();
            if (phone.equals(entry.getValue().getPhone())) {
                map.remove(entry.getKey());
                return true;
            }
        }
        return false;
    }

    /**
     * 通过判断乘客是否已经登录
     *
     * @param phone phone
     * @return 判断结果
     */
    public static boolean isContain(String phone) {
        for (CustomerSession customerSession : map.values()) {
            if (phone.equals(customerSession.getPhone())) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断session是否已经存在
     *
     * @param sessionId sessionId
     * @return 判断结果
     */
    public static boolean isExist(String sessionId) {
        return map.containsKey(sessionId);
    }

    /**
     * 通过手机号像乘客推送信息
     *
     * @param phone   phone
     * @param message message
     * @return 推送结果
     */
    public static boolean sendMessageByPhone(String phone, String message) {
        for (CustomerSession customerSession : map.values()) {
            if (phone.equals(customerSession.getPhone())) {
                customerSession.getSession().getAsyncRemote().sendText(message);
                return true;
            }
        }
        return false;
    }

    /**
     * 通过customerId向乘客推送信息
     *
     * @param customerId customerId
     * @param message    message
     * @return 推送结果
     */
    public static boolean sendMessageById(String customerId, String message) {
        for (CustomerSession customerSession : map.values()) {
            if (customerId.equals(customerSession.getId())) {
                customerSession.getSession().getAsyncRemote().sendText(message);
                return true;
            }
        }
        return false;
    }

    /**
     * 获取customer
     *
     * @param sessionId sessionId
     * @return customerSession
     */
    public static CustomerSession getCustomerSession(String sessionId) {
        return map.get(sessionId);
    }

    /**
     * 通过sessionID 获取customer
     *
     * @param customerId customerID
     * @return customerSession
     */
    public static CustomerSession getSessionByCid(int customerId) {
        for (CustomerSession session : map.values()) {
            if (customerId == session.getId()) {
                return session;
            }
        }
        return null;
    }

    /**
     * 通过phone获取customer
     *
     * @param phone phone
     * @return customerSession
     */
    public static CustomerSession getSessionByPhone(String phone) {
        for (CustomerSession session : map.values()) {
            if (phone.equals(session.getPhone())) {
                return session;
            }
        }
        return null;
    }

    /**
     * 通过name获取customer
     *
     * @param name name
     * @return customerSession
     */
    public static CustomerSession getSessionByName(String name) {
        for (CustomerSession session : map.values()) {
            if (name.equals(session.getName())) {
                return session;
            }
        }
        return null;
    }

    /**
     * 获取所有customer
     *
     * @return list
     */
    public static List getAll() {
        List list = new LinkedList();
        for (CustomerSession customerSession : map.values()) {
            list.add(customerSession.Cclone());
        }
        return list;
    }
}
